package com.gitee.swsk33.providerdemo.handler;

import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 发生限流后的异常处理
 */
@RestControllerAdvice
public class SentinelHandler {

	/**
	 * 当发生限流后会发生FlowException异常，捕获该异常自定义限流后返回
	 * FlowException是BlockException的子类，因此捕捉BlockException也是可以的
	 */
	@ExceptionHandler(FlowException.class)
	public String blockException(HttpServletResponse response) {
		response.setStatus(429);
		return "太快了！";
	}

}